package com.ruibang.glass.produce.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.domain.DictResp;
import com.ruibang.glass.produce.constant.Constants;
import com.ruibang.glass.produce.domain.req.RackChangeLogReq;
import com.ruibang.glass.produce.entity.RackChangeLog;
import com.ruibang.glass.produce.mapper.RackChangeLogMapper;
import com.ruibang.glass.produce.service.RackChangeLogService;
import com.teaming.cloud.framework2.autoconfigure.dynamic.datasource.annotation.DS;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-01-31  16:17
 * @Description: TODO
 * @Version: 1.0
 */


@Slf4j
@Service
@DS("cold1_bc2mes")
public class RackChangeLogServiceImpl extends ServiceImpl<RackChangeLogMapper, RackChangeLog> implements RackChangeLogService {

    @Override
    public Page<RackChangeLog> queryBz(RackChangeLogReq rackChangeLogReq) {
        Page<RackChangeLog> page = lambdaQuery()
                .like(StringUtils.isNotBlank(rackChangeLogReq.getStationId()), RackChangeLog::getStationId, "%" + rackChangeLogReq.getStationId() + "%")
                .like(StringUtils.isNotBlank(rackChangeLogReq.getRackId()), RackChangeLog::getRackId, "%" + rackChangeLogReq.getRackId() + "%")
                .like(StringUtils.isNotBlank(rackChangeLogReq.getPort()), RackChangeLog::getPort, "%" + rackChangeLogReq.getPort() + "%")
                .like(StringUtils.isNotBlank(rackChangeLogReq.getRackUseBatch()), RackChangeLog::getRackUseBatch, "%" + rackChangeLogReq.getRackUseBatch() + "%")
                .eq(StringUtils.isNotBlank(rackChangeLogReq.getPz()), RackChangeLog::getPz, rackChangeLogReq.getPz())
                .between(rackChangeLogReq.getSjStartTime() != null && rackChangeLogReq.getSjEndTime() != null, RackChangeLog::getUpTimeFormat, rackChangeLogReq.getSjStartTime(), rackChangeLogReq.getSjEndTime())
                .between(rackChangeLogReq.getXjStartTime() != null && rackChangeLogReq.getXjEndTime() != null, RackChangeLog::getDownTimeFormat, rackChangeLogReq.getXjStartTime(), rackChangeLogReq.getXjEndTime())
                .like(RackChangeLog::getStationId, "BZ%")
                .orderByDesc(RackChangeLog::getUpTimeFormat)
                .page(new Page<>(rackChangeLogReq.getPageNo(), rackChangeLogReq.getPageSize()));
        return setPzInfo(page);
    }

    @Override
    public Page<RackChangeLog> queryTr(RackChangeLogReq rackChangeLogReq) {
        Page<RackChangeLog> page = lambdaQuery()
                .like(StringUtils.isNotBlank(rackChangeLogReq.getStationId()), RackChangeLog::getStationId, "%" + rackChangeLogReq.getStationId() + "%")
                .like(StringUtils.isNotBlank(rackChangeLogReq.getRackId()), RackChangeLog::getRackId, "%" + rackChangeLogReq.getRackId() + "%")
                .like(StringUtils.isNotBlank(rackChangeLogReq.getPort()), RackChangeLog::getPort, "%" + rackChangeLogReq.getPort() + "%")
                .like(StringUtils.isNotBlank(rackChangeLogReq.getRackUseBatch()), RackChangeLog::getRackUseBatch, "%" + rackChangeLogReq.getRackUseBatch() + "%")
                .eq(StringUtils.isNotBlank(rackChangeLogReq.getPz()), RackChangeLog::getPz, rackChangeLogReq.getPz())
                .between(rackChangeLogReq.getSjStartTime() != null && rackChangeLogReq.getSjEndTime() != null, RackChangeLog::getUpTimeFormat, rackChangeLogReq.getSjStartTime(), rackChangeLogReq.getSjEndTime())
                .between(rackChangeLogReq.getXjStartTime() != null && rackChangeLogReq.getXjEndTime() != null, RackChangeLog::getDownTimeFormat, rackChangeLogReq.getXjStartTime(), rackChangeLogReq.getXjEndTime())
                .like(RackChangeLog::getStationId, "TR%")
                .orderByDesc(RackChangeLog::getUpTimeFormat)
                .page(new Page<>(rackChangeLogReq.getPageNo(), rackChangeLogReq.getPageSize()));
        return setPzInfo(page);
    }

    private Page<RackChangeLog> setPzInfo(Page<RackChangeLog> page) {
        List<RackChangeLog> records = page.getRecords();
        if (CollectionUtils.isNotEmpty(records)) {
            //获取字典对应关系
            List<DictResp> dictInfo = baseMapper.getTrBzDict();
            Map<String, DictResp> dictMap = dictInfo.stream().collect(Collectors.toMap(DictResp::getValue, e -> e, (o, n) -> n));
            for (RackChangeLog rackChangeLog : records) {
                String pz = rackChangeLog.getPz();
                DictResp dictResp = dictMap.get(pz);
                if (dictResp != null) {
                    rackChangeLog.setPz(dictResp.getName());
                }
            }
        }
        return page;
    }
}
